const path = require('path');
const fsPromise = require('fs-promise');
const RSATool = require('../lib/RSATool.js');
let configInit = false;

const configTool = {
    appBasePath: '',
    wwwPath: '',
    config: {} as any,
    async editConfig(key: string, value: any) {
        await this.init();
        let result = false;
        if (typeof this.config[key] === typeof value) {
            this.config[key] = value;
            await fsPromise.writeJSON(path.join(this.appBasePath, '/conf/config.json'), this.config);
            result = true;
        }
        return result;
    },
    async init() {
        if (!configInit) {
            configInit = true;

            // 是否处于开发环境
            let isDev = !__dirname.includes('snapshot');

            if (isDev) {
                this.appBasePath = path.join(__dirname, '../../');
            } else {
                this.appBasePath = path.dirname(process.execPath);
            }

            await fsPromise.ensureDir(path.join(this.appBasePath, '/conf'));
            await fsPromise.ensureDir(path.join(this.appBasePath, '/temp/fileOut'));
            await fsPromise.ensureDir(path.join(this.appBasePath, '/temp/fileDB'));
            await fsPromise.ensureDir(path.join(this.appBasePath, '/temp/logs/http'));
            await fsPromise.ensureDir(path.join(this.appBasePath, '/www'));

            let configFile = path.join(this.appBasePath, '/conf/config.json');

            // 判断是读取配置文件
            await fsPromise.readJson(configFile).then((d: any) => {
                this.config = d;

                // 判断配置文件开启了安全模块，并且还未初始化安全模块
                if (this.config.safeModule.state && this.config.safeModule.publicDer === '' && this.config.safeModule.privateDer === '') {
                    let rsa = RSATool.createKey();
                    this.config.safeModule.privateDer = rsa.privateDer;
                    this.config.safeModule.publicDer = rsa.publicDer;
                    fsPromise.writeJSON(configFile, this.config);
                } else if (!this.config.safeModule.state && (this.config.safeModule.publicDer || this.config.safeModule.privateDer)) {
                    this.config.safeModule.privateDer = '';
                    this.config.safeModule.publicDer = '';
                    fsPromise.writeJSON(configFile, this.config);
                }


            }).catch(() => {
                // 新生成一份配置文件
                this.config = {
                    'version': 'v1.7',
                    'clientServerPort': 3000,
                    'manageServerPort': 3001,
                    'wwwPath': 'www',
                    'prefixHTTPList': [],
                    'HTTPSProxy': {
                        'port': null,
                        'certPath': '/cert/localhost.cert',
                        'keyPath': '/cert/localhost.key'
                    },
                    'CacheSave': {
                        'state': true,
                        'CacheCORS': true,
                        'CacheMapBox': true,
                        'CacheOSM': true,
                        'CacheCesiumTerrain': true,
                        'CacheSaveFile': true,
                        'CacheSaveDB': false
                    },
                    'safeModule': {
                        'state': false,
                        'publicDer': '',
                        'privateDer': ''
                    },
                    // v1.7版 试用令牌 2022-09-01 ~ 2022-11-1
                    'WinUIAuth': '3dd6e5a3c7655c5c8291acd1e714953252fa8d44b74e2cb5d2fcddbad93a4fd11b446ca1a01e6847311a5b051b938957b5fff5c7d57a296991c4181de58324eb66752868b79b26a492d732c929df37cfc0a010f86362ffa136acb03540c906e4c41bc8bf74e9c55989a2f65f8f27eb9a8233588de48bc8d1c4934cb3bc7f071fa69432477b0692fe511934bd8a69fa2dfc833835e914760794d7c01f91154796a9b75fc7619787610763de60ce25a0ad6de71e99243192a331482e76f880bfce34cb6bf3da4ab8ef2bb5c942b2d4e36d3cf1ba7899cb41b1225a56b7de7e754c7c1ff898074813fb0342086eaaf7e69e20c8ede9c87d439eb1cb3f0f49ac600f7382288828076e37d018bf3e01c9f27a804f02f1ca8caeeb21ce7e3a71a5d763f1aeac0f0bcb96a4fed56843458a1c56c8cd47a257715fa519771b20f51d8bf0ce3e2749e50ca4de63ccec8a5b9956b20cc9250e8253f17f5b7e59aea58b796d8c2bf653faaf3c40aa81f5b6cee00eb3a6e173c8891a69523c81fba34711372b49e28c64aeae5b12c07593512ddca2f063d170e9edbc494e1a9c3025bf6c5a6cbc3a940b160abeefb7891664ad85f8b5b488863fd9f5fb6f13d83b5598af296452bf00bdfc8b947c27f60ab2f35d3ee1c27ee96d4a556240497ea7ec01ff3bfee6e0a9e5480056d4109abd4fdf44ade3685718f9e3303fdb731731b3a636b106'
                };
                fsPromise.writeJSON(configFile, this.config);
            });

            this.wwwPath = path.join(this.appBasePath, this.config.wwwPath);
        }
    },
    preservableONDB() {
        return this.config.CacheSave.state && this.config.CacheSave.CacheSaveDB;
    },
    preservableONFile() {
        return this.config.CacheSave.state && this.config.CacheSave.CacheSaveFile;
    }
};


export = configTool;
